package org.dragonnova.meetingserver.server.application;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;

import org.dragonnova.meetingserver.R;
import org.dragonnova.meetingserver.server.activity.MainActivity;
import org.dragonnova.meetingserver.utils.LogUtil;
import org.dragonnova.meetingserver.utils.PromptUtil;

import java.util.Random;


public class TransBoxApplication extends Application {

    public static TransBoxApplication instance;

    private static String mCurrentUserName = "hucn";
    private static Boolean mIsLogin = true;

    private static boolean mainActivityVisible;
    private static boolean devicesActivityVisible;

    public static int MAINACTIVITY_STATUS = 0;
    public static int DEVICESACTIVITY_STATUS = 1;


    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        PromptUtil.setContext(getApplicationContext());

        //=====================应用不被杀死的代码========================
        //程序崩溃时触发线程
        //Thread.setDefaultUncaughtExceptionHandler(restartHandler);
        //===============================================================
    }

    //创建服务用于捕获崩溃异常
    private Thread.UncaughtExceptionHandler restartHandler = new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread thread, Throwable ex) {
            LogUtil.eee("crash happens!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

            PromptUtil.poolPlay(getApplicationContext(), R.raw.application_crashed);

            //发生崩溃异常时,重启应用
            restartApp();
        }
    };

    public void restartApp() {
        LogUtil.eee("restart the app when crash happens or the app forced stoppd by user");

        Intent intent = new Intent(instance.getApplicationContext(), MainActivity.class);
        PendingIntent restartIntent = PendingIntent.getActivity(
                instance.getApplicationContext(), (new Random().nextInt(100)), intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager mgr = (AlarmManager) instance.getSystemService(Context.ALARM_SERVICE);
        // 0.2秒钟后重启应用
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 200, restartIntent);
        //结束进程之前可以把你程序的注销或者退出代码放在这段代码之前
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    public boolean isActivityVisible() {
        return mainActivityVisible || devicesActivityVisible;
    }

    public void activityResumed(int i) {
        if (i == MAINACTIVITY_STATUS) {
            mainActivityVisible = true;
        } else if (i == DEVICESACTIVITY_STATUS) {
            devicesActivityVisible = true;
        }
    }

    public void activityPaused(int i) {
        if (i == MAINACTIVITY_STATUS) {
            mainActivityVisible = false;
        } else if (i == DEVICESACTIVITY_STATUS) {
            devicesActivityVisible = false;
        }
        if (!mainActivityVisible && !devicesActivityVisible) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (!mainActivityVisible) {
                        if (activityVisibleListener != null) activityVisibleListener.onInvisible();
                    }
                }
            }, 100);
        }
    }

    private activityVisibleListener activityVisibleListener;

    public interface activityVisibleListener {
        void onInvisible();
    }

    public void setactivityVisibleListener(activityVisibleListener visibleListener) {
        this.activityVisibleListener = visibleListener;
    }

    public static TransBoxApplication getInstance() {
        return instance;
    }

    /**
     * 返回域名
     *
     * @return String
     */
    public String getCurrentUserName() {
        return mCurrentUserName;
    }

    /**
     * 获取登录状态
     *
     * @return boolean
     */
    public boolean getCurrentIsLogin() {
        return mIsLogin;
    }
}
